﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Control_DSelYearMonth : System.Web.UI.UserControl
{
    private int _StartOffsetYear = 2;
    private int _EndOffsetYear = 2;
    private char _Separator = '-';
    private string _SelectYear = "";
    private string _SelectMonth = "";
    private string _SelectYearMonth = "";
    private bool _AddNoNeedSelYear = true;
    private bool _AddNoNeedSelMonth = true;

    /// <summary>
    /// 添加不需要选择年的选项
    /// </summary>
    public bool AddNoNeedSelYear
    {
        get
        {
            return _AddNoNeedSelYear;
        }
        set
        {
            _AddNoNeedSelYear = value;
        }
    }

    /// <summary>
    /// 添加不需要选择月的选项
    /// </summary>
    public bool AddNoNeedSelMonth
    {
        get
        {
            return _AddNoNeedSelMonth;
        }
        set
        {
            _AddNoNeedSelMonth = value;
        }
    }

    /// <summary>
    /// 年和月之间的分割符
    /// </summary>
    public char Separator
    {
        get
        {
            return _Separator;
        }
        set
        {
            _Separator = value;
        }
    }
       

    /// <summary>
    /// 控件是否可操作
    /// </summary>
    public bool Enabled
    {
        get
        {
            return SelYear.Enabled;
        }
        set
        {
            SelYear.Enabled = value;
            SelMonth.Enabled = value;
        }
    }

    /// <summary>
    /// 显示起始的时间与现在的偏离值
    /// </summary>
    public int StartOffsetYear
    {
        get
        {
            return _StartOffsetYear;
        }
        set
        {
            _StartOffsetYear = value;
        }
    }

    /// <summary>
    /// 显示终止的时间与现在的偏离值
    /// </summary>
    public int EndOffsetYear
    {
        get
        {
            return _EndOffsetYear;
        }
        set
        {
            _EndOffsetYear = value;
        }
    }

    /// <summary>
    /// 当前选择的时间年值
    /// </summary>
    public string SelectYear
    {
        get
        {
            return SelYear.SelectedValue;
        }
        set
        {
            _SelectYear = value;
            if (SelYear.Items.FindByValue(_SelectYear) != null) SelYear.SelectedValue = _SelectYear;
        }
    }

    /// <summary>
    /// 当前选择的时间月值
    /// </summary>
    public string SelectMonth
    {
        get
        {
            return SelMonth.SelectedValue;
        }
        set
        {
            _SelectMonth = value;
            if (SelMonth.Items.FindByValue(_SelectMonth) != null) SelMonth.SelectedValue = _SelectMonth;
        }
    }

    /// <summary>
    /// 当前选择的时间年-月值
    /// </summary>
    public string SelectYearMonth
    {
        get
        {
            string SelDate = SelYear.SelectedValue + _Separator + SelMonth.SelectedValue;
            return SelDate;
        }
        set
        {
            _SelectYearMonth = value;
                      
            string[] Sp = _SelectYearMonth.Split(_Separator);
            if (Sp.Length > 1)
            {
                _SelectYear = Sp[0];
                if (SelYear.Items.FindByValue(_SelectYear) != null) SelYear.SelectedValue = _SelectYear;
                _SelectMonth = Sp[1];
                if (SelMonth.Items.FindByValue(_SelectMonth) != null) SelMonth.SelectedValue = _SelectMonth;
            }
        }
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillData();
        }
        else
        {
            if (SelYear.Items.Count == 0)
            {
                FillData();
            }
        }
    }

    /// <summary>
    /// 填仓数据
    /// </summary>
    private void FillData()
    {
        //  add the first yaer
        if (_AddNoNeedSelYear == true)
        {
            ListItem dt = new ListItem("请选择", "0");
            SelYear.Items.Add(dt);
        }

        int FirstYear = DateTime.Now.Year;
        FirstYear = FirstYear - _StartOffsetYear;
        int LastYear = FirstYear + _StartOffsetYear + _EndOffsetYear;

        for (int i = FirstYear; i <= LastYear; i++)
        {
            ListItem dt1 = new ListItem(i.ToString().Trim()+ "年", i.ToString().Trim());
            SelYear.Items.Add(dt1);
        }

        if (_AddNoNeedSelYear == true)
        {
            if (SelYear.Items.FindByValue(_SelectYear) != null) SelYear.SelectedValue = _SelectYear;
        }
        else
        {
            if (_SelectYear == null || _SelectYear.Length == 0)
            {
                _SelectYear = DateTime.Now.Year.ToString().Trim();
            }
            if (SelYear.Items.FindByValue(_SelectYear) != null) SelYear.SelectedValue = _SelectYear;
        }


        if (_AddNoNeedSelMonth == true)
        {
            ListItem dt3 = new ListItem("请选择", "0");
            SelMonth.Items.Add(dt3);
        }
        for (int i = 1; i <= 12; i++)
        {
            ListItem dt2 = new ListItem(i.ToString().Trim() + "月", i.ToString().Trim());
            SelMonth.Items.Add(dt2);
        }

        if (_AddNoNeedSelMonth == true)
        {
            if (SelMonth.Items.FindByValue(_SelectMonth) != null) SelMonth.SelectedValue = _SelectMonth;
        }
        else
        {
            if (_SelectMonth == null || _SelectMonth.Length == 0)
            {
                _SelectMonth = DateTime.Now.Month.ToString().Trim();
            }

            if (SelMonth.Items.FindByValue(_SelectMonth) != null) SelMonth.SelectedValue = _SelectMonth;
        }

    }
}
